/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Weber;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IWeberService;
import org.cx1.service.api.IWeberService;
import org.cx1.service.impl.DWeberService;
import org.cx1.web.controller.action.WeberAction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class WeberController extends BaseController {
    
    private IWeberService weberService = new DWeberService();
    
    public WeberController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            case WeberAction.READ_ALL: {
                readAll(mav, map, request);
                break;
            }
            case WeberAction.READ_ALL_BY_LIST_NAME: {
                readAllByListName(mav, map, request);
                break;
            }
            case WeberAction.CREATE: {
                add(mav, map, request);
                break;
            }
            case WeberAction.DELETE: {
                delete(mav, map, request);
                break;
            }
            case WeberAction.SEND_EMAIL_TO_LIST: {
                sendEmailToList(mav, map, request);
                break;
            }
            default: {
                readAll(mav, map, request);
                break;
            }
        }        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        
        mav.addObject("model", map);
        return mav;
    }
    
    private void readAll(ModelAndView mav, Map map, HttpServletRequest request) {
        String strOffset = request.getParameter("offset");
        String strLimit = request.getParameter("limit");
        
        int offset = StringUtils.isEmpty(strOffset) ? 0 : Integer.parseInt(strOffset);
        int limit = StringUtils.isEmpty(strLimit) ? 10 : Integer.parseInt(strLimit);
                
        ServiceResponse<List<Weber>> response = weberService.readAll(offset, 999999);
        map.put("webers", response.getResponse());
        mav.setViewName("weberall");
    }
        
    private void readAllByListName(ModelAndView mav, Map map, HttpServletRequest request) {
        String strOffset = request.getParameter("offset");
        String strLimit = request.getParameter("limit");
        
        int offset = StringUtils.isEmpty(strOffset) ? 0 : Integer.parseInt(strOffset);
        int limit = StringUtils.isEmpty(strLimit) ? 10 : Integer.parseInt(strLimit);
        
        String listName = request.getParameter("listName");
        ServiceResponse<List<Weber>> response = weberService.readAllByListName(listName, offset, 999999);
        map.put("listName", listName);
        map.put("webers", response.getResponse());
        mav.setViewName("weberlistall");
    }
    
    private void add(ModelAndView mav, Map map, HttpServletRequest request) {  
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
            String listName = request.getParameter("listName");
            String email = request.getParameter("email");
            String multiEmail = request.getParameter("multiEmail");
            
            Weber weber = new Weber();
            weber.setListName(listName.trim());
            weber.setEmail(email.trim());            
            weberService.create(weber);
            
            if (!StringUtils.isEmpty(multiEmail)) {
                String[] emailArr = multiEmail.split("\n");
                if (emailArr.length > 0) {
                    for (int i = 0; i < emailArr.length; i++) {
                        Weber weber1 = new Weber();
                        weber1.setListName(listName.trim());
                        weber1.setEmail(emailArr[i].trim());            
                        weberService.create(weber1); 
                    }
                }
            }
            mav.setView(new RedirectView("weber.htm"));
        }
        else {
            mav.setViewName("weberall");            
        }
    }
    
    private void delete(ModelAndView mav, Map map, HttpServletRequest request) {
        String listName = request.getParameter("listName");
        String email = request.getParameter("email");
        ServiceResponse<Boolean> response = weberService.delete(listName, email);
        mav.setView(new RedirectView("weber.htm"));
    }
    
    private void sendEmailToList(ModelAndView mav, Map map, HttpServletRequest request) {
        String listName = request.getParameter("listName");
        String title = request.getParameter("title");
        String body = request.getParameter("body");
        ServiceResponse<Boolean> response = weberService.sendEmailToList(title, body, listName, 0, 999999);
        mav.setView(new RedirectView("weber.htm"));
    }
}